package javolution.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.realtime.MemoryArea;
import javolution.JavolutionError;
import javolution.context.LogContext;
import javolution.context.ObjectFactory;
import javolution.context.PersistentContext;
import javolution.lang.MathLib;
import javolution.lang.Realtime;
import javolution.lang.Reusable;
import javolution.text.Text;
import javolution.util.FastCollection;
import javolution.xml.XMLSerializable;

/* loaded from: classes2.dex */
public class FastMap<K, V> implements Map<K, V>, Reusable, XMLSerializable, Realtime {
    private static final int B0 = 4;
    private static final int B1 = 10;
    private static final int B2 = 6;
    private static final int C0 = 16;
    private static final int C1 = 1024;
    private static final int C2 = 64;
    private static final long serialVersionUID = 1;
    private transient Entry<K, V>[] _entries;
    private transient FastMap<K, V>.EntrySet _entrySet;
    private transient Entry<K, V> _head;
    private transient boolean _isShared;
    private transient FastComparator _keyComp;
    private transient FastComparator _keyComparator;
    private transient FastMap<K, V>.KeySet _keySet;
    private transient int _keyShift;
    private transient int _nullCount;
    private transient int _size;
    private transient FastMap[] _subMaps;
    private transient Entry<K, V> _tail;
    private transient Map<K, V> _unmodifiable;
    private transient boolean _useSubMaps;
    private transient FastComparator _valueComparator;
    private transient FastMap<K, V>.Values _values;
    private static final Entry[] NULL_ENTRIES = new Entry[1024];
    private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastMap.8
        @Override // javolution.context.ObjectFactory
        public void cleanup(Object obj) {
            ((FastMap) obj).reset();
        }

        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new FastMap();
        }
    };

    /* loaded from: classes2.dex */
    public static class Entry<K, V> implements Map.Entry<K, V>, FastCollection.Record {
        public static final Entry NULL = new Entry();
        private K _key;
        private int _keyHash;
        private Entry<K, V> _next;
        private Entry<K, V> _previous;
        private V _value;

        protected Entry() {
        }

        @Override // java.util.Map.Entry
        public boolean equals(Object obj) {
            if (obj instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) obj;
                return FastComparator.DEFAULT.areEqual(this._key, entry.getKey()) && FastComparator.DEFAULT.areEqual(this._value, entry.getValue());
            }
            return false;
        }

        @Override // java.util.Map.Entry
        public final K getKey() {
            return this._key;
        }

        @Override // javolution.util.FastCollection.Record
        public final Entry<K, V> getNext() {
            return this._next;
        }

        @Override // javolution.util.FastCollection.Record
        public final Entry<K, V> getPrevious() {
            return this._previous;
        }

        @Override // java.util.Map.Entry
        public final V getValue() {
            return this._value;
        }

        @Override // java.util.Map.Entry
        public int hashCode() {
            K k = this._key;
            int hashCode = k != null ? k.hashCode() : 0;
            V v = this._value;
            return hashCode ^ (v != null ? v.hashCode() : 0);
        }

        @Override // java.util.Map.Entry
        public final V setValue(V v) {
            V v2 = this._value;
            this._value = v;
            return v2;
        }
    }

    /* loaded from: classes2.dex */
    private static final class EntryIterator implements Iterator {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastMap.EntryIterator.1
            @Override // javolution.context.ObjectFactory
            protected void cleanup(Object obj) {
                EntryIterator entryIterator = (EntryIterator) obj;
                entryIterator._map = null;
                entryIterator._current = null;
                entryIterator._next = null;
                entryIterator._tail = null;
            }

            @Override // javolution.context.ObjectFactory
            protected Object create() {
                return new EntryIterator();
            }
        };
        private Entry _current;
        private FastMap _map;
        private Entry _next;
        private Entry _tail;

        private EntryIterator() {
        }

        public static EntryIterator valueOf(FastMap fastMap) {
            EntryIterator entryIterator = (EntryIterator) FACTORY.object();
            entryIterator._map = fastMap;
            entryIterator._next = fastMap._head._next;
            entryIterator._tail = fastMap._tail;
            return entryIterator;
        }

        @Override // java.util.Iterator
        public boolean hasNext() {
            return this._next != this._tail;
        }

        @Override // java.util.Iterator
        public Object next() {
            Entry entry = this._next;
            if (entry != this._tail) {
                this._current = entry;
                this._next = entry._next;
                return this._current;
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.Iterator
        public void remove() {
            Entry entry = this._current;
            if (entry == null) {
                throw new IllegalStateException();
            }
            this._next = entry._next;
            this._map.remove(this._current._key);
            this._current = null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public final class EntrySet extends FastCollection implements Set {
        private final FastComparator _entryComparator;

        private EntrySet() {
            this._entryComparator = new FastComparator() { // from class: javolution.util.FastMap.EntrySet.1
                @Override // javolution.util.FastComparator
                public boolean areEqual(Object obj, Object obj2) {
                    if (!(obj instanceof Map.Entry) || !(obj2 instanceof Map.Entry)) {
                        return obj == null && obj2 == null;
                    }
                    Map.Entry entry = (Map.Entry) obj;
                    Map.Entry entry2 = (Map.Entry) obj2;
                    return FastMap.this._keyComparator.areEqual(entry.getKey(), entry2.getKey()) && FastMap.this._valueComparator.areEqual(entry.getValue(), entry2.getValue());
                }

                @Override // javolution.util.FastComparator, java.util.Comparator
                public int compare(Object obj, Object obj2) {
                    return FastMap.this._keyComparator.compare(obj, obj2);
                }

                @Override // javolution.util.FastComparator
                public int hashCodeOf(Object obj) {
                    Map.Entry entry = (Map.Entry) obj;
                    return FastMap.this._keyComparator.hashCodeOf(entry.getKey()) + FastMap.this._valueComparator.hashCodeOf(entry.getValue());
                }
            };
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public void clear() {
            FastMap.this.clear();
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public boolean contains(Object obj) {
            if (obj instanceof Map.Entry) {
                Entry entry = (Entry) obj;
                Entry<K, V> entry2 = FastMap.this.getEntry(entry.getKey());
                if (entry2 == null) {
                    return false;
                }
                return FastMap.this._valueComparator.areEqual(entry2.getValue(), entry.getValue());
            }
            return false;
        }

        @Override // javolution.util.FastCollection
        public void delete(FastCollection.Record record) {
            FastMap.this.remove(((Entry) record).getKey());
        }

        @Override // javolution.util.FastCollection
        public FastComparator getValueComparator() {
            return this._entryComparator;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record head() {
            return FastMap.this._head;
        }

        @Override // javolution.util.FastCollection, java.util.Collection, java.lang.Iterable
        public Iterator iterator() {
            return EntryIterator.valueOf(FastMap.this);
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public int size() {
            return FastMap.this._size;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record tail() {
            return FastMap.this._tail;
        }

        @Override // javolution.util.FastCollection, javolution.lang.Realtime
        public Text toText() {
            Text valueOf = Text.valueOf('[');
            Text valueOf2 = Text.valueOf('=');
            Text valueOf3 = Text.valueOf((Object) ", ");
            Entry entry = FastMap.this._head;
            Entry entry2 = FastMap.this._tail;
            while (true) {
                entry = entry._next;
                if (entry == entry2) {
                    return valueOf.concat(Text.valueOf(']'));
                }
                valueOf = valueOf.concat(Text.valueOf(entry._key)).concat(valueOf2).concat(Text.valueOf(entry._value));
                if (entry._next != entry2) {
                    valueOf = valueOf.concat(valueOf3);
                }
            }
        }

        @Override // javolution.util.FastCollection
        public Object valueOf(FastCollection.Record record) {
            return (Map.Entry) record;
        }
    }

    /* loaded from: classes2.dex */
    private static final class KeyIterator implements Iterator {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastMap.KeyIterator.1
            @Override // javolution.context.ObjectFactory
            protected void cleanup(Object obj) {
                KeyIterator keyIterator = (KeyIterator) obj;
                keyIterator._map = null;
                keyIterator._current = null;
                keyIterator._next = null;
                keyIterator._tail = null;
            }

            @Override // javolution.context.ObjectFactory
            protected Object create() {
                return new KeyIterator();
            }
        };
        private Entry _current;
        private FastMap _map;
        private Entry _next;
        private Entry _tail;

        private KeyIterator() {
        }

        public static KeyIterator valueOf(FastMap fastMap) {
            KeyIterator keyIterator = (KeyIterator) FACTORY.object();
            keyIterator._map = fastMap;
            keyIterator._next = fastMap._head._next;
            keyIterator._tail = fastMap._tail;
            return keyIterator;
        }

        @Override // java.util.Iterator
        public boolean hasNext() {
            return this._next != this._tail;
        }

        @Override // java.util.Iterator
        public Object next() {
            Entry entry = this._next;
            if (entry != this._tail) {
                this._current = entry;
                this._next = entry._next;
                return this._current._key;
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.Iterator
        public void remove() {
            Entry entry = this._current;
            if (entry == null) {
                throw new IllegalStateException();
            }
            this._next = entry._next;
            this._map.remove(this._current._key);
            this._current = null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public final class KeySet extends FastCollection implements Set {
        private KeySet() {
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public void clear() {
            FastMap.this.clear();
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public boolean contains(Object obj) {
            return FastMap.this.containsKey(obj);
        }

        @Override // javolution.util.FastCollection
        public void delete(FastCollection.Record record) {
            FastMap.this.remove(((Entry) record).getKey());
        }

        @Override // javolution.util.FastCollection
        public FastComparator getValueComparator() {
            return FastMap.this._keyComparator;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record head() {
            return FastMap.this._head;
        }

        @Override // javolution.util.FastCollection, java.util.Collection, java.lang.Iterable
        public Iterator iterator() {
            return KeyIterator.valueOf(FastMap.this);
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public boolean remove(Object obj) {
            return FastMap.this.remove(obj) != null;
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public int size() {
            return FastMap.this._size;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record tail() {
            return FastMap.this._tail;
        }

        @Override // javolution.util.FastCollection
        public Object valueOf(FastCollection.Record record) {
            return ((Entry) record)._key;
        }
    }

    /* loaded from: classes2.dex */
    private final class Unmodifiable implements Map, Serializable {
        private Unmodifiable() {
        }

        @Override // java.util.Map
        public void clear() {
            throw new UnsupportedOperationException("Unmodifiable map");
        }

        @Override // java.util.Map
        public boolean containsKey(Object obj) {
            return FastMap.this.containsKey(obj);
        }

        @Override // java.util.Map
        public boolean containsValue(Object obj) {
            return FastMap.this.containsValue(obj);
        }

        @Override // java.util.Map
        public Set entrySet() {
            throw new UnsupportedOperationException("Direct view over unmodifiable map entries is not supported  (to prevent access to Entry.setValue(Object) method). To iterate over unmodifiable map entries, applications may use the keySet() and values() fast collection views in conjonction.");
        }

        @Override // java.util.Map
        public boolean equals(Object obj) {
            return FastMap.this.equals(obj);
        }

        @Override // java.util.Map
        public Object get(Object obj) {
            return FastMap.this.get(obj);
        }

        @Override // java.util.Map
        public int hashCode() {
            return FastMap.this.hashCode();
        }

        @Override // java.util.Map
        public boolean isEmpty() {
            return FastMap.this.isEmpty();
        }

        @Override // java.util.Map
        public Set keySet() {
            return (Set) ((KeySet) FastMap.this.keySet()).unmodifiable();
        }

        @Override // java.util.Map
        public Object put(Object obj, Object obj2) {
            throw new UnsupportedOperationException("Unmodifiable map");
        }

        @Override // java.util.Map
        public void putAll(Map map) {
            throw new UnsupportedOperationException("Unmodifiable map");
        }

        @Override // java.util.Map
        public Object remove(Object obj) {
            throw new UnsupportedOperationException("Unmodifiable map");
        }

        @Override // java.util.Map
        public int size() {
            return FastMap.this.size();
        }

        public Text toText() {
            return FastMap.this.toText();
        }

        @Override // java.util.Map
        public Collection values() {
            return ((Values) FastMap.this.values()).unmodifiable();
        }
    }

    /* loaded from: classes2.dex */
    private static final class ValueIterator implements Iterator {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastMap.ValueIterator.1
            @Override // javolution.context.ObjectFactory
            protected void cleanup(Object obj) {
                ValueIterator valueIterator = (ValueIterator) obj;
                valueIterator._map = null;
                valueIterator._current = null;
                valueIterator._next = null;
                valueIterator._tail = null;
            }

            @Override // javolution.context.ObjectFactory
            protected Object create() {
                return new ValueIterator();
            }
        };
        private Entry _current;
        private FastMap _map;
        private Entry _next;
        private Entry _tail;

        private ValueIterator() {
        }

        public static ValueIterator valueOf(FastMap fastMap) {
            ValueIterator valueIterator = (ValueIterator) FACTORY.object();
            valueIterator._map = fastMap;
            valueIterator._next = fastMap._head._next;
            valueIterator._tail = fastMap._tail;
            return valueIterator;
        }

        @Override // java.util.Iterator
        public boolean hasNext() {
            return this._next != this._tail;
        }

        @Override // java.util.Iterator
        public Object next() {
            Entry entry = this._next;
            if (entry != this._tail) {
                this._current = entry;
                this._next = entry._next;
                return this._current._value;
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.Iterator
        public void remove() {
            Entry entry = this._current;
            if (entry == null) {
                throw new IllegalStateException();
            }
            this._next = entry._next;
            this._map.remove(this._current._key);
            this._current = null;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public final class Values extends FastCollection {
        private Values() {
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public void clear() {
            FastMap.this.clear();
        }

        @Override // javolution.util.FastCollection
        public void delete(FastCollection.Record record) {
            FastMap.this.remove(((Entry) record).getKey());
        }

        @Override // javolution.util.FastCollection
        public FastComparator getValueComparator() {
            return FastMap.this._valueComparator;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record head() {
            return FastMap.this._head;
        }

        @Override // javolution.util.FastCollection, java.util.Collection, java.lang.Iterable
        public Iterator iterator() {
            return ValueIterator.valueOf(FastMap.this);
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public int size() {
            return FastMap.this._size;
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record tail() {
            return FastMap.this._tail;
        }

        @Override // javolution.util.FastCollection
        public Object valueOf(FastCollection.Record record) {
            return ((Entry) record)._value;
        }
    }

    public FastMap() {
        this(4);
    }

    public FastMap(int i) {
        this._keyComparator = FastComparator.DEFAULT;
        this._valueComparator = FastComparator.DEFAULT;
        this._keyComp = FastComparator._Rehash ? FastComparator.REHASH : null;
        setup(i);
    }

    public FastMap(String str) {
        this();
        new PersistentContext.Reference(str, this) { // from class: javolution.util.FastMap.1
            @Override // javolution.context.PersistentContext.Reference
            protected void notifyChange() {
                FastMap.this.clear();
                FastMap.this.putAll((FastMap) get());
            }
        };
    }

    public FastMap(Map<? extends K, ? extends V> map) {
        this(map.size());
        putAll(map);
    }

    private FastMap(Entry[] entryArr) {
        this._keyComparator = FastComparator.DEFAULT;
        this._valueComparator = FastComparator.DEFAULT;
        this._keyComp = FastComparator._Rehash ? FastComparator.REHASH : null;
        this._entries = entryArr;
    }

    private void addEntry(int i, K k, V v) {
        if (((Entry) this._tail)._next == null) {
            createNewEntries();
        }
        Entry<K, V> entry = ((Entry) this._tail)._next;
        ((Entry) this._tail)._key = k;
        ((Entry) this._tail)._value = v;
        ((Entry) this._tail)._keyHash = i;
        mapEntry(this._tail);
        this._tail = entry;
    }

    private synchronized void clearShared() {
        ((Entry) this._head)._next = this._tail;
        ((Entry) this._tail)._previous = this._head;
        this._entries = new Entry[16];
        if (this._useSubMaps) {
            this._useSubMaps = false;
            for (int i = 0; i < 64; i++) {
                this._subMaps[i] = new FastMap(new Entry[16]);
            }
        }
        this._size = 0;
        this._nullCount = 0;
    }

    private void clearTables() {
        if (this._useSubMaps) {
            for (int i = 0; i < 64; i++) {
                this._subMaps[i].clearTables();
            }
            this._useSubMaps = false;
        }
        Entry[] entryArr = NULL_ENTRIES;
        Entry<K, V>[] entryArr2 = this._entries;
        System.arraycopy(entryArr, 0, entryArr2, 0, entryArr2.length);
        this._size = 0;
        this._nullCount = 0;
    }

    private void createNewEntries() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastMap.2
            @Override // java.lang.Runnable
            public void run() {
                Entry<K, V> newEntry = FastMap.this.newEntry();
                ((Entry) newEntry)._previous = FastMap.this._tail;
                FastMap.this._tail._next = newEntry;
                Entry<K, V> newEntry2 = FastMap.this.newEntry();
                ((Entry) newEntry2)._previous = newEntry;
                ((Entry) newEntry)._next = newEntry2;
                Entry<K, V> newEntry3 = FastMap.this.newEntry();
                ((Entry) newEntry3)._previous = newEntry2;
                ((Entry) newEntry2)._next = newEntry3;
                Entry<K, V> newEntry4 = FastMap.this.newEntry();
                ((Entry) newEntry4)._previous = newEntry3;
                ((Entry) newEntry3)._next = newEntry4;
            }
        });
    }

    /* JADX WARN: Code restructure failed: missing block: B:24:0x0049, code lost:
        return r0;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private final javolution.util.FastMap.Entry getEntry(java.lang.Object r6, int r7) {
        /*
            r5 = this;
            int r0 = r5._keyShift
            int r0 = r7 >> r0
            boolean r1 = r5._useSubMaps
            if (r1 == 0) goto L13
            javolution.util.FastMap[] r1 = r5._subMaps
            r0 = r0 & 63
            r0 = r1[r0]
            javolution.util.FastMap$Entry r6 = r0.getEntry(r6, r7)
            return r6
        L13:
            javolution.util.FastMap$Entry<K, V>[] r1 = r5._entries
        L15:
            int r2 = r0 + 1
            int r3 = r1.length
            int r3 = r3 + (-1)
            r0 = r0 & r3
            r0 = r1[r0]
            if (r0 != 0) goto L21
            r6 = 0
            return r6
        L21:
            java.lang.Object r3 = javolution.util.FastMap.Entry.access$300(r0)
            if (r6 == r3) goto L49
            int r3 = javolution.util.FastMap.Entry.access$400(r0)
            if (r7 != r3) goto L47
            javolution.util.FastComparator r3 = r5._keyComp
            if (r3 != 0) goto L3c
            java.lang.Object r3 = javolution.util.FastMap.Entry.access$300(r0)
            boolean r3 = r6.equals(r3)
            if (r3 == 0) goto L47
            goto L49
        L3c:
            java.lang.Object r4 = javolution.util.FastMap.Entry.access$300(r0)
            boolean r3 = r3.areEqual(r6, r4)
            if (r3 == 0) goto L47
            goto L49
        L47:
            r0 = r2
            goto L15
        L49:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: javolution.util.FastMap.getEntry(java.lang.Object, int):javolution.util.FastMap$Entry");
    }

    private int getMaximumDistance() {
        int i = 0;
        if (this._useSubMaps) {
            int i2 = 0;
            while (i < 64) {
                i2 = MathLib.max(i2, this._subMaps[i].getMaximumDistance());
                i++;
            }
            return i2;
        }
        int i3 = 0;
        while (true) {
            Entry<K, V>[] entryArr = this._entries;
            if (i >= entryArr.length) {
                return i3;
            }
            Entry<K, V> entry = entryArr[i];
            if (entry != null) {
                int slotOf = i - slotOf(entry, entryArr, this._keyShift);
                if (slotOf < 0) {
                    slotOf += this._entries.length;
                }
                if (slotOf > i3) {
                    i3 = slotOf;
                }
            }
            i++;
        }
    }

    private int getSubMapDepth() {
        if (this._useSubMaps) {
            int i = 0;
            for (int i2 = 0; i2 < 64; i2++) {
                i = MathLib.max(i, this._subMaps[i2].getSubMapDepth());
            }
            return i + 1;
        }
        return 0;
    }

    private long getSumDistance() {
        int i = 0;
        long j = 0;
        if (this._useSubMaps) {
            while (i < 64) {
                j += this._subMaps[i].getSumDistance();
                i++;
            }
            return j;
        }
        while (true) {
            Entry<K, V>[] entryArr = this._entries;
            if (i >= entryArr.length) {
                return j;
            }
            Entry<K, V> entry = entryArr[i];
            if (entry != null) {
                int slotOf = i - slotOf(entry, entryArr, this._keyShift);
                if (slotOf < 0) {
                    slotOf += this._entries.length;
                }
                j += slotOf;
            }
            i++;
        }
    }

    private static int indexOf(Entry entry, Entry[] entryArr, int i) {
        int slotOf = slotOf(entry, entryArr, i);
        while (true) {
            Entry entry2 = entryArr[slotOf];
            if (entry2 == entry) {
                return slotOf;
            }
            if (entry2 == null) {
                throw new JavolutionError("Concurrent maps should be marked shared");
            }
            slotOf = (slotOf + 1) & (entryArr.length - 1);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    public void mapEntry(Entry entry) {
        Entry<K, V>[] entryArr;
        int length;
        this._size++;
        int i = entry._keyHash >> this._keyShift;
        if (this._useSubMaps) {
            this._subMaps[i & 63].mapEntry(entry);
            return;
        }
        while (true) {
            int i2 = i + 1;
            length = i & (entryArr.length - 1);
            Entry<K, V> entry2 = this._entries[length];
            if (entry2 == null || entry2 == Entry.NULL) {
                break;
            }
            i = i2;
        }
        Entry<K, V>[] entryArr2 = this._entries;
        entryArr2[length] = entry;
        if (this._size + this._nullCount > (entryArr2.length >> 1)) {
            resizeTable();
        }
    }

    public static <K, V> FastMap<K, V> newInstance() {
        return (FastMap) FACTORY.object();
    }

    private synchronized V putShared(K k, V v, int i) {
        Entry entry = getEntry(k, i);
        if (entry == null) {
            addEntry(i, k, v);
            return null;
        }
        V v2 = (V) entry._value;
        entry._value = v;
        return v2;
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        setKeyComparator((FastComparator) objectInputStream.readObject());
        setValueComparator((FastComparator) objectInputStream.readObject());
        setShared(objectInputStream.readBoolean());
        int readInt = objectInputStream.readInt();
        setup(readInt);
        for (int i = 0; i < readInt; i++) {
            Object readObject = objectInputStream.readObject();
            addEntry(this._keyComparator.hashCodeOf(readObject), readObject, objectInputStream.readObject());
        }
    }

    public static void recycle(FastMap fastMap) {
        FACTORY.recycle(fastMap);
    }

    private final void removeEntry(Entry entry) {
        entry._previous._next = entry._next;
        entry._next._previous = entry._previous;
        unmapEntry(entry);
        if (this._isShared) {
            return;
        }
        entry._key = null;
        entry._value = null;
        Entry entry2 = ((Entry) this._tail)._next;
        entry._previous = this._tail;
        entry._next = entry2;
        ((Entry) this._tail)._next = entry;
        if (entry2 != null) {
            entry2._previous = entry;
        }
    }

    private synchronized V removeShared(Object obj, int i) {
        Entry entry = getEntry(obj, i);
        if (entry == null) {
            return null;
        }
        V v = (V) entry._value;
        removeEntry(entry);
        return v;
    }

    private void resizeTable() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastMap.3
            @Override // java.lang.Runnable
            public void run() {
                int i;
                boolean z;
                int length = FastMap.this._nullCount > FastMap.this._size ? FastMap.this._entries.length : FastMap.this._entries.length << 1;
                int i2 = 0;
                FastMap.this._nullCount = 0;
                if (length > 1024) {
                    if (FastMap.this._subMaps == null) {
                        FastMap.this._subMaps = new FastMap[64];
                        for (int i3 = 0; i3 < 64; i3++) {
                            FastMap fastMap = new FastMap(new Entry[16]);
                            fastMap._keyShift = FastMap.this._keyShift + 6;
                            FastMap.this._subMaps[i3] = fastMap;
                        }
                    }
                    int i4 = 0;
                    while (true) {
                        if (i4 >= FastMap.this._entries.length) {
                            z = true;
                            break;
                        }
                        int i5 = i4 + 1;
                        Entry entry = FastMap.this._entries[i4];
                        if (entry != null && entry != Entry.NULL) {
                            FastMap fastMap2 = FastMap.this._subMaps[(entry._keyHash >> FastMap.this._keyShift) & 63];
                            if (fastMap2._size >= (FastMap.this._size >> 1)) {
                                z = false;
                                break;
                            }
                            fastMap2.mapEntry(entry);
                        }
                        i4 = i5;
                    }
                    if (z) {
                        FastMap.this._useSubMaps = true;
                        return;
                    } else {
                        LogContext.warning("Uneven hash distribution");
                        FastMap.this._subMaps = null;
                    }
                }
                Entry[] entryArr = new Entry[length];
                while (i2 < FastMap.this._entries.length) {
                    int i6 = i2 + 1;
                    Entry entry2 = FastMap.this._entries[i2];
                    if (entry2 != null && entry2 != Entry.NULL) {
                        int i7 = entry2._keyHash >> FastMap.this._keyShift;
                        while (true) {
                            i = (length - 1) & i7;
                            if (entryArr[i] == null) {
                                break;
                            }
                            i7++;
                        }
                        entryArr[i] = entry2;
                    }
                    i2 = i6;
                }
                FastMap.this._entries = entryArr;
            }
        });
    }

    private void setup(int i) {
        int i2 = 16;
        while (i2 < i) {
            i2 <<= 1;
        }
        this._entries = new Entry[MathLib.min(i2 << 1, 1024)];
        this._head = newEntry();
        this._tail = newEntry();
        ((Entry) this._head)._next = this._tail;
        ((Entry) this._tail)._previous = this._head;
        Entry<K, V> entry = this._tail;
        int i3 = 0;
        while (true) {
            int i4 = i3 + 1;
            if (i3 >= i) {
                return;
            }
            Entry<K, V> newEntry = newEntry();
            ((Entry) newEntry)._previous = entry;
            ((Entry) entry)._next = newEntry;
            entry = newEntry;
            i3 = i4;
        }
    }

    private static int slotOf(Entry entry, Entry[] entryArr, int i) {
        return (entry._keyHash >> i) & (entryArr.length - 1);
    }

    private void unmapEntry(Entry entry) {
        this._size--;
        if (this._useSubMaps) {
            this._subMaps[(entry._keyHash >> this._keyShift) & 63].unmapEntry(entry);
            return;
        }
        Entry<K, V>[] entryArr = this._entries;
        entryArr[indexOf(entry, entryArr, this._keyShift)] = Entry.NULL;
        this._nullCount++;
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.writeObject(getKeyComparator());
        objectOutputStream.writeObject(getValueComparator());
        objectOutputStream.writeBoolean(this._isShared);
        objectOutputStream.writeInt(this._size);
        Entry<K, V> entry = this._head;
        Entry<K, V> entry2 = this._tail;
        while (true) {
            entry = ((Entry) entry)._next;
            if (entry == entry2) {
                return;
            }
            objectOutputStream.writeObject(((Entry) entry)._key);
            objectOutputStream.writeObject(((Entry) entry)._value);
        }
    }

    @Override // java.util.Map
    public final void clear() {
        if (this._isShared) {
            clearShared();
            return;
        }
        Entry<K, V> entry = this._head;
        Entry<K, V> entry2 = this._tail;
        while (true) {
            entry = ((Entry) entry)._next;
            if (entry == entry2) {
                this._tail = ((Entry) this._head)._next;
                clearTables();
                return;
            }
            ((Entry) entry)._key = null;
            ((Entry) entry)._value = null;
        }
    }

    @Override // java.util.Map
    public final boolean containsKey(Object obj) {
        return getEntry(obj) != null;
    }

    @Override // java.util.Map
    public final boolean containsValue(Object obj) {
        return values().contains(obj);
    }

    @Override // java.util.Map
    public final Set<Map.Entry<K, V>> entrySet() {
        if (this._entrySet == null) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastMap.5
                @Override // java.lang.Runnable
                public void run() {
                    FastMap fastMap = FastMap.this;
                    fastMap._entrySet = new EntrySet();
                }
            });
        }
        return this._entrySet;
    }

    @Override // java.util.Map
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof Map) {
            return entrySet().equals(((Map) obj).entrySet());
        }
        return false;
    }

    @Override // java.util.Map
    public final V get(Object obj) {
        FastComparator fastComparator = this._keyComp;
        Entry entry = getEntry(obj, fastComparator == null ? obj.hashCode() : fastComparator.hashCodeOf(obj));
        if (entry != null) {
            return (V) entry._value;
        }
        return null;
    }

    public final Entry<K, V> getEntry(Object obj) {
        FastComparator fastComparator = this._keyComp;
        return getEntry(obj, fastComparator == null ? obj.hashCode() : fastComparator.hashCodeOf(obj));
    }

    public FastComparator<? super K> getKeyComparator() {
        return this._keyComparator;
    }

    public FastComparator<? super V> getValueComparator() {
        return this._valueComparator;
    }

    @Override // java.util.Map
    public int hashCode() {
        Entry<K, V> entry = this._head;
        Entry<K, V> entry2 = this._tail;
        int i = 0;
        while (true) {
            entry = ((Entry) entry)._next;
            if (entry == entry2) {
                return i;
            }
            i += entry.hashCode();
        }
    }

    public final Entry<K, V> head() {
        return this._head;
    }

    @Override // java.util.Map
    public final boolean isEmpty() {
        return ((Entry) this._head)._next == this._tail;
    }

    public boolean isShared() {
        return this._isShared;
    }

    @Override // java.util.Map
    public final Set<K> keySet() {
        if (this._keySet == null) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastMap.6
                @Override // java.lang.Runnable
                public void run() {
                    FastMap fastMap = FastMap.this;
                    fastMap._keySet = new KeySet();
                }
            });
        }
        return this._keySet;
    }

    protected Entry<K, V> newEntry() {
        return new Entry<>();
    }

    public void printStatistics(PrintStream printStream) {
        long sumDistance = getSumDistance();
        int i = this._size;
        int i2 = i != 0 ? (int) (sumDistance / i) : 0;
        synchronized (printStream) {
            printStream.print("SIZE: " + this._size);
            printStream.print(", TABLE LENGTH: " + this._entries.length);
            printStream.print(", USE SUB-MAPS: " + this._useSubMaps);
            printStream.print(", SUB-MAPS DEPTH: " + getSubMapDepth());
            printStream.print(", NULL COUNT: " + this._nullCount);
            printStream.print(", IS SHARED: " + this._isShared);
            printStream.print(", AVG DISTANCE: " + i2);
            printStream.print(", MAX DISTANCE: " + getMaximumDistance());
            printStream.println();
        }
    }

    @Override // java.util.Map
    public final V put(K k, V v) {
        FastComparator fastComparator = this._keyComp;
        int hashCode = fastComparator == null ? k.hashCode() : fastComparator.hashCodeOf(k);
        Entry entry = getEntry(k, hashCode);
        if (entry != null) {
            V v2 = (V) entry._value;
            entry._value = v;
            return v2;
        } else if (this._isShared) {
            return putShared(k, v, hashCode);
        } else {
            addEntry(hashCode, k, v);
            return null;
        }
    }

    @Override // java.util.Map
    public final void putAll(Map<? extends K, ? extends V> map) {
        for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    @Override // java.util.Map
    public final V putIfAbsent(K k, V v) {
        FastComparator fastComparator = this._keyComp;
        int hashCode = fastComparator == null ? k.hashCode() : fastComparator.hashCodeOf(k);
        Entry entry = getEntry(k, hashCode);
        if (entry != null) {
            return (V) entry._value;
        }
        if (!this._isShared) {
            addEntry(hashCode, k, v);
            return null;
        }
        synchronized (this) {
            Entry entry2 = getEntry(k, hashCode);
            if (entry2 != null) {
                return (V) entry2._value;
            }
            addEntry(hashCode, k, v);
            return null;
        }
    }

    @Override // java.util.Map
    public final V remove(Object obj) {
        FastComparator fastComparator = this._keyComp;
        int hashCode = fastComparator == null ? obj.hashCode() : fastComparator.hashCodeOf(obj);
        Entry entry = getEntry(obj, hashCode);
        if (entry == null) {
            return null;
        }
        if (this._isShared) {
            return removeShared(obj, hashCode);
        }
        V v = (V) entry._value;
        removeEntry(entry);
        return v;
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        setShared(false);
        clear();
        setKeyComparator(FastComparator.DEFAULT);
        setValueComparator(FastComparator.DEFAULT);
    }

    /* JADX WARN: Code restructure failed: missing block: B:8:0x0010, code lost:
        if ((r3 instanceof javolution.util.FastComparator.Direct) != false) goto L9;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public javolution.util.FastMap<K, V> setKeyComparator(javolution.util.FastComparator<? super K> r3) {
        /*
            r2 = this;
            r2._keyComparator = r3
            boolean r0 = r3 instanceof javolution.util.FastComparator.Default
            r1 = 0
            if (r0 == 0) goto Le
            boolean r3 = javolution.util.FastComparator._Rehash
            if (r3 == 0) goto L12
            javolution.util.FastComparator<java.lang.Object> r3 = javolution.util.FastComparator.REHASH
            goto L13
        Le:
            boolean r0 = r3 instanceof javolution.util.FastComparator.Direct
            if (r0 == 0) goto L13
        L12:
            r3 = r1
        L13:
            r2._keyComp = r3
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: javolution.util.FastMap.setKeyComparator(javolution.util.FastComparator):javolution.util.FastMap");
    }

    public FastMap<K, V> setShared(boolean z) {
        this._isShared = z;
        return this;
    }

    public FastMap<K, V> setValueComparator(FastComparator<? super V> fastComparator) {
        this._valueComparator = fastComparator;
        return this;
    }

    @Override // java.util.Map
    public final int size() {
        return this._size;
    }

    public final Entry<K, V> tail() {
        return this._tail;
    }

    public final String toString() {
        return toText().toString();
    }

    @Override // javolution.lang.Realtime
    public Text toText() {
        return Text.valueOf(entrySet());
    }

    public final Map<K, V> unmodifiable() {
        if (this._unmodifiable == null) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastMap.7
                @Override // java.lang.Runnable
                public void run() {
                    FastMap fastMap = FastMap.this;
                    fastMap._unmodifiable = new Unmodifiable();
                }
            });
        }
        return this._unmodifiable;
    }

    @Override // java.util.Map
    public final Collection<V> values() {
        if (this._values == null) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastMap.4
                @Override // java.lang.Runnable
                public void run() {
                    FastMap fastMap = FastMap.this;
                    fastMap._values = new Values();
                }
            });
        }
        return this._values;
    }
}
